#include "List.h";
ListNode * ListCreate(LTDateType x)
{

ListNode * newnode	 = (ListNode*)malloc(sizeof(ListNode));
if (newnode==NULL)
	{
		printf("malooc fail");
		exit(-1);
	}
newnode->x = x;
newnode->prev = NULL;
newnode->next = NULL;
return newnode;
}

ListNode* ListInit(ListNode* phead)
{
	assert(phead);
	phead = ListCreate(0);
	phead->next = phead;
	phead->prev = phead;
	return phead;
}
void ListPushBack(LTNode* phead, LTDateType x)
{
	assert(phead);
LTNode*newnode=	ListCreate(x);
LTNode* tail = phead->prev;
tail->next = newnode;
newnode->prev = tail;
newnode->next = phead;
phead->prev = newnode;
}
void ListPopBack(ListNode* phead)
{
	assert(phead);
	assert(phead->next != phead);
		ListNode* tail = phead->prev;
		ListNode* newtail = tail->prev;
		free(tail);
		tail = NULL;
		newtail->next = phead;
		phead->next = newtail;
}
void ListInsert(ListNode * pos,LTDateType x)
{
	assert(pos);
	ListNode * newnode=ListCreate(x);
	ListNode* prepos = pos->prev;
	prepos->next = newnode;
	newnode->prev = prepos;
	newnode->next = pos;
	pos->prev = newnode;
	
}
void ListPushFront(LTNode* phead,  LTDateType x)
{
	assert(phead);
	ListInsert(phead->next, x);
}
LTNode* ListFind( LTNode * phead  ,LTDateType x)
{
	assert(phead);
	LTNode* findpos = phead;
	while (findpos != phead)
	{
		if (findpos->x == x)
		{
			return findpos;
		}
		findpos = findpos->next;
	}
	return NULL;
}
void ListPrint(LTNode* phead)
{
	assert(phead);
	LTNode* pphead = phead->next;
	while (pphead!=phead)
	{
		printf("%d ", pphead->x);
		pphead = pphead->next;
	}
	printf("\n\n");
}
void ListNodeDestroy(LTNode * phead)
{
	assert(phead);
	LTNode* pphead = phead->next;
	while(pphead != pphead)
	{
		LTNode* nextpphead=pphead->next;
		free(pphead);
		pphead = nextpphead;
	}
}
void LIstNodeErase(LTNode* pos)
{
	assert(pos);
	LTNode* prev = pos->prev;
	LTNode* next = pos->next;
	free(pos);
	pos = NULL;
	prev->next = next;
	next->prev = prev;
}
void PopFront(ListNode* phead)
{
	assert(phead);
	assert(phead->next != phead);
	LIstNodeErase(phead->next);

}